import tkinter as tk

# master = tk.Tk()
#
# master.title("图形化界面")
#
# master.geometry("400x400")
#
# master.resizable(False, False)
#
# entry = tk.Entry(master, width=30)
# entry.pack(padx=10, pady=10)
# def get_value():
#     value = entry.get()
#     print("输入内容:", value)
# button = tk.Button(master, text="提交", command=get_value)
# button.pack()
#
# frame = tk.Frame(master)
# frame.pack()
# top = tk.Label(frame, text="顶部", font=("Arial", 20, "bold"))
# top.pack(side='top')
# bottom = tk.Label(frame, text="底部", font=("Arial", 20, "bold"))
# bottom.pack(side='bottom')
# left = tk.Label(frame, text="左侧", font=("Arial", 20, "bold"))
# left.pack(side='left')
# right = tk.Label(frame, text="右侧", font=("Arial", 20, "bold"))
# right.pack(side='right')
#
# master.mainloop()






# root = tk.Tk()
# root.title('图形化界面')
# label = tk.Label(root, text='Hello World')
# label.pack()
# root.mainloop()



from tkinter import scrolledtext

def add_to_display():
    # 获取输入框内容
    input_text = entry.get()
    if input_text.strip():  # 非空验证
        # 动态添加到文本显示框（尾部追加+换行）
        display_box.insert(tk.END, input_text + "\n")
        # 自动滚动至底部
        display_box.see(tk.END)
        # 清空输入框
        entry.delete(0, tk.END)
    else:
        display_box.insert(tk.END, "⚠️ 输入为空！\n")

# 创建主窗口
root = tk.Tk()
root.title("动态内容展示框")
root.geometry("500x400")

# 输入框（支持中文）
entry = tk.Entry(
    root,
    width=40,
    font=("Microsoft YaHei", 12),
    bg="#FFFFE0"  # 浅黄色背景
)
# pady=15, padx=10, fill=tk.X
entry.pack()

# 提交按钮
submit_btn = tk.Button(
    root,
    text="添加到显示框",
    command=add_to_display,
    bg="#4CAF50",  # 绿色背景
    fg="white",
    font=("Microsoft YaHei", 10, "bold")
)
submit_btn.pack(pady=5)

# 动态显示框（带滚动条）
display_box = scrolledtext.ScrolledText(
    root,
    wrap=tk.WORD,       # 按单词换行
    width=50,
    height=15,
    font=("Microsoft YaHei", 10),
    bg="#F5F5F5"        # 浅灰色背景
)
display_box.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)

# 绑定回车键提交
entry.bind("<Return>", lambda event: add_to_display())

root.mainloop()
